<?
$num_pages = 0;
$info = $GLOBALS['info'];
//print_r($info);
$options = $GLOBALS['options'];
$options['group_by'] = array('order_id', 'order_number', 'order_date', 'sum_amount_total', 'payment_method', 'payment_currency', 'client_id', 'client_username', 'client_name', 'client_country');
$options['order_by'] = 'order_id';
$options['order_by_'] = 'DESC';

if(!$options['limit'] or !$options['offset']){
	$options['limit'] = 50;
	$options['offset'] = 0;
}

$payment_methods = $GLOBALS['payment_methods'];

if($info){
	foreach($info as $k => $v){
		if($v){
			$search_key = $k;
			$search_value = $v;
			$params[$k] = $v;
			break;
		}
	}
	//$params = $info;
	$params['payment_method'] = $payment_methods[($params['payment_method'])];
	$params['sale_type'] = "^!='reversal'";
	//$params['amount_total'] = '^>0';
	$params['client_username'] = '%'.$params['client_username'];
	$params['client_name'] = '%'.$params['client_name'];
	$search_results = getGeneralResults($params, $options);
	//print_r(getGeneralResults($params, $options, 'query'));
}

if($search_results['num_rows']){
	$num_pages = ceil($search_results['num_rows'] / $options['limit']);
	$page = $options['offset'] / $options['limit'] + 1;
}

$search_select = array(
	'order_id' => 'Order ID',
	'order_date' => 'Date',
	'sum_amount_total' => 'Amount',
	'payment_currency' => 'Payment Currency',
	'payment_method' => 'Payment Gateway',
	'order_number' => 'Transaction ID',
	'client_username' => 'Client\'s Username',
	'client_name' => 'Client\'s Name',
	'client_country' => 'Client\'s Country'
);
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td align="left"><strong>He who seeks, usually finds!</strong><br>
	Search the options separately or in combination.<br>
	Don't forget that when you have an issued Invoice followed by a Credit Note, the unifying factor is always the Order ID.
	</td>
	<td align="right"></td>
  </tr>
</table>
<br>
<form id="searchForm" name="searchForm" onSubmit="return false;">
<table style="width:auto;" border="0" cellspacing="0" cellpadding="0">
  <tr onMouseOver="show_filter();" onMouseOut="hide_filter();">
	<td valign="top"><strong>Search by:</strong></td>
	<td valign="top">
		<select name="select" id='select_params' onChange="toggle('display', this.value, 'values');">
		<?
		foreach($search_select as $k => $v){
			echo "<option value='$k' id='selected_$k'>$v</option>";
		}
		?>
		</select>
	</td>
	<td valign="top" id="values">
		<div id="order_id" style="display:block;">
			<input size=10 name="info[order_id]" id='info_order_id' type="text" maxlength="6" value="<?=$info[order_id];?>" />
		</div>
		<div id="order_date" style="display:none;">
			<a href="#" title="Order Date" onClick="popUpCalendar(searchForm, searchForm.info_order_date, searchForm.info_order_date, 'yyyy-mm-dd');return false;"><img src="images/calendar.gif" alt="Select Date" width="18" height="14" border="0"></a>
			<input size=10 name="info[order_date]" id='info_order_date' type="text" maxlength="10" value="<?=$info[order_date];?>" />
		</div>
		<div id="sum_amount_total" style="display:none;">
			<input size=10 name="info[sum_amount_total]" id='info_sum_amount_total' type="text" maxlength="8" value="<?=$info[sum_amount_total];?>" /><br>
		</div>
		<div id="payment_currency" style="display:none;">
			<select name="info[payment_currency]" id='info_payment_currency'>
			<option value="">All</option>
			<?
			foreach ($GLOBALS['currencies'] as $k => $v){
				if($info['payment_currency'] == $k){
					$selected = 'selected';
				}else{
					$selected = '';
				}
				echo "<option value='$k' $selected>$k</option>";
			}
			?>
			</select>
		</div>
		<div id="payment_method" style="display:none;">
			<select name="info[payment_method]" id='info_payment_method'>
				<option value="">All</option>
				<option value="paypal" <?if($info['payment_method'] == 'paypal'){ echo 'selected';}?>>PayPal</option>
				<option value="worldpay" <?if($info['payment_method'] == 'worldpay'){ echo 'selected';}?>>WorldPay</option>
				<option value="2co" <?if($info['payment_method'] == '2co'){ echo 'selected';}?>>2CheckOut</option>
				<option value="wallet" <?if($info['payment_method'] == 'wallet'){ echo 'selected';}?>>My Wallet</option>
				<option value="other" <?if($info['payment_method'] == 'other'){ echo 'selected';}?>>Other</option>
			</select>
		</div>
		<div id="order_number" style="display:none;">
			<input name="info[order_number]" id='info_order_number' type="text" maxlength="20" value="<?=$info[order_number];?>" />
		</div>
		<div id="client_username" style="display:none;">
			<input name="info[client_username]" id='info_client_username' type="text" maxlength="10" value="<?=$info[client_username];?>" />
		</div>
		<div id="client_name" style="display:none;">
			<input name="info[client_name]" id='info_client_name' type="text" maxlength="64" value="<?=$info[client_name];?>" />
		</div>
		<div id="client_country" style="display:none;">
			<select name="info[client_country]" id='info_client_country'>
			<option></option>
			<?
			if(preg_match('/^[A-Z]{2}$/', $info['client_country'])){
				$cntr = $info['client_country'];
			}else{
				$cntr = 'blabla';
			}

			echo get_countries($cntr);
			?>
			</select>
		</div>
	</td>
	<td valign="top"><input type="submit" value="Search" onClick="unsetLimitOffset();openPage('search.html', getFormData('searchForm'));" id="submit_button"></td>
  </tr>
  <tr>
	<td></td>
	<td colspan=2>
		<div id='filter' onMouseOver="show_filter();" onMouseOut="hide_filter();"></div>
	</td>
	<td></td>
  </tr>
</table>
<input type='hidden' name='num_pages' value='<?=$num_pages?>' id='num_pages'>
<input type='hidden' name='options[limit]' value='<?=$options['limit']?>' id='limit'>
<input type='hidden' name='options[offset]' value='<?=$options['offset']?>' id='offset'>
<input type='hidden' id='searchFormId' value='searchForm'>
</form>
<?
if($search_results['result']){
/*
	if($search_value){
		if($search_key != 'client_country'){
			$str = "for <strong>".$search_select[$search_key]." <span class='red'>$search_value</span></strong>";
		}else{
			$str = "for <strong>".$search_select[$search_key]." <span class='red'>".get_country_name($search_results['result'][0]['client_country'])."</span></strong>";
		}
	}
*/
	echo "<p><img src='images/arrow_red.jpg' alt='Results' width='9' height='9'> Results $str</p>
	<table class='bTable' cellspacing=0 cellpadding=0 width=100%>
		<tr>
			<th>Date</th>
			<th>Order ID</th>
			<th>Amount</th>
			<th>Gateway Transaction ID</th>
			<th>Username</th>
			<th>Client</th>
		</tr>";
	foreach($search_results['result'] as $k => $v){
		$order_date = date('M d, Y', strtotime($v['order_date']));
		$transaction = $GLOBALS['inv_p1'][($GLOBALS['p2'][($v['payment_method'])])].' ID '.$v['order_number'];
		$retail = $GLOBALS['currencies'][($v['payment_currency'])].' '.$v['sum_amount_total'];
		echo "<tr>
			<td align='center'>$order_date</td>
			<td align='center'><a href='#' onClick=\"setSearchValue('$search_key', '$search_value');openPage('account(billing).html', 'info[order_id]=$v[order_id]&info[client_id]=$v[client_id]' + '&return_to=openPage(\'search.html\',\'' + getFormData('searchForm').replace(/&/g,':amp:') + '\');'); return false;\">$v[order_id]</a></td>
			<td align='right' style='padding-right:5px;'>$retail</td>
			<td>$transaction</td>
			<td align='center'><a href='#' onClick=\"setSearchValue('$search_key', '$search_value');openPage('account(billing).html', 'info[client_id]=$v[client_id]' + '&return_to=openPage(\'lists.html\',\'' + getFormData('searchForm').replace(/&/g,':amp:') + '\');'); return false;\">$v[client_username]</a></td>
			<td align='center'>$v[client_name]</td>
		  </tr>";
	}
	echo '</table>';
?>
<br />
<table align="center" width="80%"  border="0" cellspacing="0" cellpadding="0">
	<tr>
	  <td height="21" width="21">
		<?
		if($page == 1){
			echo "<img src='images/result_arrow_disabled_left.jpg' alt='View Previous Results Page' width='21' height='21' border='0' />";
		}else{
			echo "<a href='#' onClick=\"setSearchValue('$search_key', '$search_value');page('prev');openPage('search.html', getFormData('searchForm'));return false;\"><img src='images/result_arrow_enabled_left.jpg' alt='View Previous Results Page' width='21' height='21' border='0' /></a>";
		}
		?>
	  </td>
	  <td width="180" class="black" style="padding-left:7px; ">Results page: <span class="red style2"><strong><?=$page?></strong></span> of <span class="black style2"><strong><?=$num_pages?></strong></span></td>
	  <td width="21">
		<?
		if($page == $num_pages){
			echo "<img src='images/result_arrow_disabled_right.jpg' alt='View Next Results Page' width='21' height='21' border='0' />";
		}else{
			echo "<a href='#' onClick=\"setSearchValue('$search_key', '$search_value');page('next');openPage('search.html', getFormData('searchForm'));return false;\"><img src='images/result_arrow_enabled_right.jpg' alt='View Next Results Page' width='21' height='21' border='0' />";
		}
		?>
	  </td>
	  <td width="23"></td>
		<td class="black" width="142" align="right" style="padding-right:7px; ">Go Directly to page: </td>
		<td width="40" align="left"><input name="" id='gotopage' class="black" style="width:34px; height: 18px; padding-left:7px; border: 1px solid #89B4CC; font-weight:bold; color:#0000CC;"></td>
		<td width="28" style="padding-left:7px; "><a href="#" onClick="setSearchValue('<?=$search_key?>', '<?=$search_value?>');page(document.getElementById('gotopage').value);openPage('search.html', getFormData('searchForm'));return false;"><img src="images/go2page_arrow.jpg" alt="View Selected Page" width="21" height="21" border="0"></a></td>
	</tr>
</table>
<?
}elseif($info){
	include($GLOBALS['path'].'no_results.html');
}
?>
<br>